package com.luoke.service.request.impl;

import cn.hutool.json.JSONUtil;
import com.luoke.connect.GameWebSocketClient;
import com.luoke.constants.ModuleTypeConstant;
import com.luoke.constants.SubmoduleTypeConstant;
import com.luoke.context.UserContext;
import com.luoke.manage.gamemanage.GameManage;
import com.luoke.pojo.dto.UserDTO;
import com.luoke.pojo.entity.User;
import com.luoke.result.Result;
import com.luoke.service.request.UserRequestService;

import java.util.Random;
import java.util.Scanner;

/**
 * 用户请求服务层
 */
public class UserRequestServiceImpl implements UserRequestService {

    /**
     * 发送给服务端的注册请求
     *
     * @param gameWebSocketClient 使用该对象与服务端通信
     * @param scanner             输入流
     */
    @Override
    public void register(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        loginAndRegister(gameWebSocketClient, scanner, SubmoduleTypeConstant.USER_REGISTER);
    }

    /**
     * 发送给服务端的登录请求
     *
     * @param gameWebSocketClient 使用该对象与服务端通信
     * @param scanner             输入流
     */
    @Override
    public void login(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        loginAndRegister(gameWebSocketClient, scanner, SubmoduleTypeConstant.USER_LOGIN);
    }

    /**
     * 注销(退出登录)
     *
     * @param gameWebSocketClient 使用该对象与服务端通信
     * @param scanner             输入流
     */
    @Override
    public void logout(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        //退出的话直接给服务端请求就好了,带一个用户名
        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.USER_LOGOUT);
        request.setData(UserContext.getCurrentUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));

    }

    @Override
    public void getBag(GameWebSocketClient gameWebSocketClient) {
        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.BAG);
        request.setData(UserContext.getCurrentUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    @Override
    public void getPets(GameWebSocketClient gameWebSocketClient) {
        Result<User> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.PETS);
        request.setData(UserContext.getUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    @Override
    public void getShop(GameWebSocketClient gameWebSocketClient) {
        Result<User> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.SHOP);
        request.setData(UserContext.getUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    @Override
    public void getMap(GameWebSocketClient gameWebSocketClient) {
        Result<User> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.MAP);
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    @Override
    public void eatAppleGame(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        GameManage gameManage = new GameManage();
        String result = gameManage.eatAppleGame();
        makeMoney(gameWebSocketClient, result);
    }

    @Override
    public void MazeGame(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        GameManage gameManage = new GameManage();
        String result = gameManage.mazeGame();
        makeMoney(gameWebSocketClient, result);
    }

    @Override
    public void TwentyFourGame(GameWebSocketClient gameWebSocketClient, Scanner scanner) {
        GameManage gameManage = new GameManage();
        String result = gameManage.twentyFourPointGame();
        makeMoney(gameWebSocketClient, result);
    }


    /**
     * 登录和注册方法大差不差，就写成一个方法好了
     *
     * @param gameWebSocketClient 使用该对象与服务端通信
     * @param scanner             输入流
     * @param command             登录或注册的指令
     */
    public void loginAndRegister(GameWebSocketClient gameWebSocketClient, Scanner scanner, String command) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        UserDTO userRequestDTO = new UserDTO(username, password);
        Result<UserDTO> userRequestDTOResult = new Result<>();
        userRequestDTOResult.setModuleName(ModuleTypeConstant.USER);
        userRequestDTOResult.setCommand(command);
        userRequestDTOResult.setData(userRequestDTO);

        //向服务端发送登录请求
        gameWebSocketClient.send(JSONUtil.toJsonStr(userRequestDTOResult));
    }


    /**
     * 赚钱请求
     * @param gameWebSocketClient 通信对象
     * @param message 信息
     */
    public void makeMoney(GameWebSocketClient gameWebSocketClient,String message){
        if (message.contains("失败")||message.contains("0")){
            //失败给0-20的游戏币
            int randomNum = new Random().nextInt(20) + 1;
            Result<Object> objectResult = new Result<>();
            objectResult.setModuleName(ModuleTypeConstant.USER);
            objectResult.setCommand(SubmoduleTypeConstant.MINI_GAME);
            objectResult.setFromUser(UserContext.getCurrentUser());
            objectResult.setData(randomNum);
            gameWebSocketClient.send(JSONUtil.toJsonStr(objectResult));
            System.out.println("通关失败，获得了" + randomNum + "游戏币");
        } else if (message.contains("成功")) {
            //成功给100游戏币
            Result<Object> objectResult = new Result<>();
            objectResult.setModuleName(ModuleTypeConstant.USER);
            objectResult.setCommand(SubmoduleTypeConstant.MINI_GAME);
            objectResult.setFromUser(UserContext.getCurrentUser());
            objectResult.setData(100);
            gameWebSocketClient.send(JSONUtil.toJsonStr(objectResult));
            System.out.println("成功通关，恭喜您获得了100游戏币");
        }
    }


}
